import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:social_sharing_plus/social_sharing_plus.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:vape_xe/services/services.dart';
import 'package:vape_xe/utils/extension.dart';
import 'package:vape_xe/values/values.dart';

class ShareDialogView extends StatefulWidget {
  const ShareDialogView({super.key});

  @override
  _ShareDialogViewState createState() => _ShareDialogViewState();
}

class _ShareDialogViewState extends State<ShareDialogView> with SingleTickerProviderStateMixin {
  late List<ShareInfo> share;

  Future<void> _share(int index) async {
    const String text = 'Hello, this is a test share!';
    //Instagram不支持暂时用linkedin
    if (share[index].type == SocialPlatform.linkedin) {
      text.copy();
      await Future.delayed(const Duration(milliseconds: 500));
      final Uri launchUri = Uri(
        scheme: 'instagram',
        host: 'share',
        // queryParameters: {
        //   'captionText': text,
        //   'caption': text,
        // },
      );
      if (await canLaunchUrl(launchUri)) {
        await launchUrl(launchUri);
      } else {
        throw 'Could not launch $launchUri';
      }
    } else {
      SocialSharingPlus.shareToSocialMedia(
        share[index].type,
        text,
        isOpenBrowser: true,
      );
    }
  }

  @override
  void initState() {
    super.initState();
    share = [
      ShareInfo(type: SocialPlatform.whatsapp, title: 'Whatsapp', image: 'ic_whatsapp'),
      ShareInfo(type: SocialPlatform.telegram, title: 'Telegram\nContacts', image: 'ic_telegram_contacts'),
      ShareInfo(type: SocialPlatform.linkedin, title: 'Instagram', image: 'ic_instagram'),
      ShareInfo(type: SocialPlatform.twitter, title: 'X.com', image: 'ic_x'),
      ShareInfo(type: SocialPlatform.facebook, title: 'Facebook', image: 'ic_facebook')
    ];
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        color: Colors.black,
        borderRadius: const BorderRadius.only(topLeft: Radius.circular(20), topRight: Radius.circular(20)),
        boxShadow: [Shadows.dialogShadow],
      ),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          23.hGap,
          Text('Share with'.tr, style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18.sp)),
          27.hGap,
          SizedBox(
            height: 100.h,
            child: ListView.separated(
              padding: EdgeInsets.only(left: 10.w),
              scrollDirection: Axis.horizontal,
              itemBuilder: (context, index) {
                return GestureDetector(
                  onTap: () {
                    _share(index);
                    Navigator.pop(context);
                  },
                  child: SizedBox(
                    width: 75.w,
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        Image.asset('assets/images/${share[index].image}.webp', width: 48.w, height: 48.w),
                        8.hGap,
                        Text(share[index].title.tr, style: TextStyle(fontSize: 12.sp))
                      ],
                    ),
                  ),
                );
              },
              separatorBuilder: (BuildContext context, int index) {
                return 10.vGap;
              },
              itemCount: share.length,
            ),
          ),
          20.hGap,
          GestureDetector(
            onTap: () {
              UserStore.to.profile.inviteCode?.copy();
              Navigator.pop(context);
            },
            child: SizedBox(
              height: 56.h,
              child: Center(
                child: Text('Copy Link'.tr, style: TextStyle(fontSize: 16.sp, fontWeight: FontWeight.bold)),
              ),
            ),
          ),
          GestureDetector(
            onTap: () => Navigator.pop(context),
            child: SizedBox(
              height: 56.h,
              child: Center(
                child: Text('Cancel'.tr, style: TextStyle(fontSize: 16.sp, fontWeight: FontWeight.bold)),
              ),
            ),
          )
        ],
      ),
    );
  }
}

class ShareInfo {
  final SocialPlatform type;
  final String title;
  final String image;

  ShareInfo({required this.type, required this.title, required this.image});
}
